﻿using NoteSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace NoteSystem
{
    public partial class Note : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["User"]!=null)
                {
                    
                    int id = Convert.ToInt32(Request["id"]);
                    if (id==0)
                    {
                        return;
                    }     
                    DataSet ds = DbHelper.Query($"select * from notes where id={id}");
                    txtTitle.Text=ds.Tables[0].Rows[0]["title"].ToString();
                    txtDetail.Value=ds.Tables[0].Rows[0]["detail"].ToString();
                }                
            }
        }

        protected void btnCommit_Click(object sender, EventArgs e)
        {
            //日志发布
            string detail = txtDetail.Value;
            DataRow userRow = (DataRow)Session["User"];
            int user_id = Convert.ToInt32( userRow["id"]);
            string title=txtTitle.Text.Trim();
            int id = Convert.ToInt32(Request["id"]);
            if (id>0)
            {
                int flag = DbHelper.ExecuteSql($@"update notes set title='{title}',detail='{detail}'where id={id}" );
               
                if (flag>0)
                {
                    Response.Write("<script>alert('修改成功')</script>");
                }
            }
            else
            {
                int flag = DbHelper.ExecuteSql($@"insert into notes values('{title}','{detail}',{user_id},'{DateTime.Now}',0)");
                if (flag>0)
                {
                    Response.Write("<script>alert('发布成功')</script>");
                }
            }
        }
    }
}